const db = require('../../db');

//创建新投诉记录
exports.addNewComplaint = (order_id,reason,type,callback) => {
    const q = "INSERT INTO `module2_complaint`(order_id,type,complain_reason) values(?,?,?);";
    const values =[order_id,type,reason];

    db.query(q, values, (err, results) => {
        if (err) {
          callback(err);
        } else if (results.affectedRows !== 1) {
          callback(new Error("更新失败，请稍后再试！"));
        } else {
          callback(null, "更新成功！", 0);
        }
    });
}

//输入投诉回复，订单号，更新投诉回复
exports.updateComplaintRefonse = (orderId,response, callback) => {
    const q = "UPDATE module2_complaint SET complain_response = ? WHERE order_id = ?;";

    const values = [response, orderId];

    db.query(q, values, (err, results) => {
        if (err) {
          callback(err);
        } else if (results.affectedRows !== 1) {
          callback(new Error("更新失败，请稍后再试！"));
        } else {
          callback(null, "更新成功！", 0);
        }
    });
};

//输入订单号，查询投诉信息
exports.ComplaintIdSearch = (req, res) => {
    const q = "Select * from module2_complaint WHERE order_id = ?;";

    const order_id = req.params.order_id; // 通过 params 传参
    db.query(q, order_id,(err, results) => {
        if (err) return(err.message)
        res.send({
          status: 0,
          message: '数据返回成功！',
          data: results,
        })
    });
};